package adminapilogic

import (
	v1 "code.bydev.io/cht/customer/kyc-stub.git/pkg/bybit/kyc/admin/v1"
	privateV1 "code.bydev.io/cht/customer/kyc-stub.git/pkg/bybit/kyc/info/v1"
	"code.bydev.io/cht/customer/kyc_admin_service/service/private/internal/svc"
	"context"
)

type AdminAPISetMaintainInfoLogic struct {
	svcCtx *svc.ServiceContext
}

func NewAdminAPISetMaintainInfoLogic(
	svcCtx *svc.ServiceContext,
) *AdminAPISetMaintainInfoLogic {
	return &AdminAPISetMaintainInfoLogic{
		svcCtx: svcCtx,
	}
}

func (l *AdminAPISetMaintainInfoLogic) SetMaintainInfo(
	ctx context.Context,
	in *v1.SetMaintainInfoRequest,
) (*v1.SetMaintainInfoResponse, error) {
	var homeMaintainInfo *privateV1.HomeMaintainInfo
	if in.GetHomeMaintainInfo() != nil {
		homeMaintainInfo = &privateV1.HomeMaintainInfo{
			ManualSwitch:    in.GetHomeMaintainInfo().GetManualSwitch(),
			ServiceMaintain: in.GetHomeMaintainInfo().GetServiceMaintain(),
		}
	}
	var providerMaintainInfo *privateV1.ProviderMaintainInfo
	if in.GetProviderMaintainInfo() != nil {
		var providerMaintainItems []*privateV1.ProviderMaintainInfo_ProviderMaintainItem
		if len(in.GetProviderMaintainInfo().GetProviderMaintainItems()) > 0 {
			for _, item := range in.GetProviderMaintainInfo().GetProviderMaintainItems() {
				providerMaintainItems = append(providerMaintainItems, &privateV1.ProviderMaintainInfo_ProviderMaintainItem{
					Provider:       item.GetProvider(),
					MaintainSwitch: item.GetMaintainSwitch(),
				})
			}
		}
		providerMaintainInfo = &privateV1.ProviderMaintainInfo{
			ManualSwitch:          in.GetProviderMaintainInfo().GetManualSwitch(),
			ProviderMaintainItems: providerMaintainItems,
		}
	}
	_, err := l.svcCtx.NothingEnv.KycSvc().SetMaintainInfo(ctx, &privateV1.SetMaintainInfoRequest{
		MaintainTime:         in.GetMaintainTime(),
		HomeMaintainInfo:     homeMaintainInfo,
		ProviderMaintainInfo: providerMaintainInfo,
	})
	if err != nil {
		return nil, err
	}
	return &v1.SetMaintainInfoResponse{}, nil
}
